package com.example.springbootjdbc.dao.impl;

import com.example.springbootjdbc.dao.UserDao;
import com.example.springbootjdbc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;

/**
 * Description:springbootweb
 * Created by admin  on 2020/4/17  16:55
 */

/**
 * 持久层
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 添加用户
     * @param user
     */
    @Override
    public void addUser(User user) {
        String sql = "insert into user(username,usersex) values(?,?)";
        jdbcTemplate.update(sql,user.getUsername(),user.getUsersex());
    }

    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<User> findAll() {
        String sql = "select * from user";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
    }
}
